package com.chentongwei.core.tucao.biz;

import com.alibaba.fastjson.JSON;
import com.chentongwei.cache.redis.IBasicCache;
import com.chentongwei.common.constant.RedisEnum;
import com.chentongwei.common.creator.ResultCreator;
import com.chentongwei.common.entity.Result;
import com.chentongwei.core.tucao.dao.ICatalogDAO;
import com.chentongwei.facade.tucao.entity.vo.ListCatalogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 容器启动时执行的Service。负责将一些data缓存到redis
 *
 * @author TongWei.Chen 2017-9-28 18:00:05
 */
@Service
public class InitCatalogBiz {
    private static final Logger LOG = LoggerFactory.getLogger(InitCatalogBiz.class);

    @Autowired
    private ICatalogDAO catalogDAO;
    @Autowired
    private IBasicCache basicCache;

    /**
     * 缓存分类列表
     *
     * @return Result
     */
    public Result initCatalog() {
        List<ListCatalogVO> list = catalogDAO.list();
        basicCache.set(RedisEnum.CATALOG.getKey(), JSON.toJSONString(list));
        LOG.info("缓存分类信息完成");
        return ResultCreator.getSuccess();
    }
}